package servletpkg;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.NewsDao;
import entity.News;

public class DoModifyNewsServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public DoModifyNewsServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		NewsDao newsdao=new NewsDao();
     	News news=new News();
    	DiskFileItemFactory df=new DiskFileItemFactory();
    	ServletFileUpload sf=new ServletFileUpload(df);
    	List<FileItem> list=null;
		try {
			list = sf.parseRequest(request);
		} catch (FileUploadException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	boolean isNull=true;
    	for(FileItem file:list){
    		if(!file.isFormField()&&file.getName().length()!=0){
    			isNull=false;
    			String picName=file.getName();
    			String houzhui=picName.substring(picName.lastIndexOf("."));
    			String newPicName=UUID.randomUUID()+houzhui;
    			String fpath=request.getSession().getServletContext().getRealPath("news/images");
    			try {
					file.write(new File(fpath,newPicName));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			String filepath="images/"+newPicName;
    			news.setNpicPath(filepath);
    		}else{
    		if(file.getFieldName().equals("ntitle")){
    			news.setNtitle(file.getString("utf-8"));
    		}else if(file.getFieldName().equals("nauthor")){
    			news.setNauthor(file.getString("utf-8"));
    		}else if(file.getFieldName().equals("nsummary")){
    			news.setNsummary(file.getString("utf-8"));
    		}else if(file.getFieldName().equals("ncontent")){
    			news.setNcontent(file.getString("utf-8"));
    		}else if(file.getFieldName().equals("ntid")){
    			news.setNtid(Integer.parseInt(file.getString("utf-8")));
    		}else if(file.getFieldName().equals("nid")){
    			news.setNid(Integer.parseInt(file.getString("utf-8")));
    		}
    		}
    	}
    	news.setNmodifyDate(new Date());
    	Integer i=null;
    	if(isNull){
    		i=newsdao.update2(news);
    	}else{
    		i=newsdao.update(news);
    	}
    	
    	request.getRequestDispatcher("AdminServlet").forward(request, response);
    	
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doGet(request, response);
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
